import asyncio
from mcp import ClientSession
from contextlib import AsyncExitStack


class MCPClient:

    def init(self):
        """初始化 MCP 客户端"""
        self.session = None  # 先不连接 MCP 服务器
        self.exit_stack = AsyncExitStack()  # 创建资源管理器

    async def connect_to_mock_server(self):
        """模拟 MCP 服务器的连接（暂不连接真实服务器）"""
        print(" ✅   MCP 客户端已初始化，但未连接到服务器")

    async def chat_loop(self):
        """运行交互式聊天循环"""
        print("\nMCP 客户端已启动！输入 'quit' 退出")

        while True:
            try:
                query = input("\nQuery: ").strip()
                if query.lower() == 'quit':
                    break
                print(f"\n疊  [Mock Response] 你说的是：{query}")
            except Exception as e:
                print(f"\n⚠  发生错误: {str(e)}")

    async def cleanup(self):
        """清理资源"""
        await self.exit_stack.aclose()


async def main():
        client = MCPClient()
        try:
            await client.connect_to_mock_server()
            await client.chat_loop()
        finally:
            await client.cleanup()

if __name__ == "__main__":
    asyncio.run(main())